home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / chistory.el < prev    next >
Lisp/Scheme  |  1993-04-02  |  7KB  |  176 lines

  1. ;;; chistory.el --- list command history
  2.  
  3. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  4.  
  5. ;; Author: K. Shane Hartman
  6. ;; Maintainer: FSF
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; This really has nothing to do with list-command-history per se, but
  27. ;; its a nice alternative to C-x ESC (repeat-complex-command) and
  28. ;; functions as a lister if given no pattern.  It's not important
  29. ;; enough to warrant a file of its own.
  30.  
  31. ;;; Code:
  32.  
  33. ;;;###autoload
  34. (defun repeat-matching-complex-command (&optional pattern)
  35.   "Edit and re-evaluate complex command with name matching PATTERN.
  36. Matching occurrences are displayed, most recent first, until you select
  37. a form for evaluation.  If PATTERN is empty (or nil), every form in the
  38. command history is offered.  The form is placed in the minibuffer for
  39. editing and the result is evaluated."
  40.   (interactive "sRedo Command (regexp): ")
  41.   (if pattern
  42.       (if (equal (setq pattern
  43.                (substring pattern
  44.                   (or (string-match "[ \t]*[^ \t]" pattern)
  45.                       (length pattern))))
  46.          "")
  47.       (setq pattern nil)))
  48.   (let ((history command-history)
  49.     (temp)
  50.     (what))
  51.     (while (and history (not what))
  52.       (setq temp (car history))
  53.       (if (and (or (not pattern) (string-match pattern (symbol-name (car temp))))
  54.            (y-or-n-p (format "Redo %s? " (setq temp (prin1-to-string temp)))))
  55.       (setq what (car history))
  56.     (setq history (cdr history))))
  57.     (if (not what)
  58.     (error "Command history exhausted")
  59.       ;; Try to remove any useless command history element for this command.
  60.       (if (eq (car (car command-history)) 'repeat-matching-complex-command)
  61.       (setq command-history (cdr command-history)))
  62.       (edit-and-eval-command "Redo: " what))))
  63.  
  64. (defvar default-command-history-filter-garbage
  65.   '(command-history-mode
  66.     list-command-history
  67.     electric-command-history)
  68.   "*A list of symbols.  If `default-list-command-history-filter' is
  69. given a list whose car is an element of this list, then it will return
  70. non-nil (indicating the list should be discarded from the history).
  71. Initially, all commands related to the command history are discarded.")
  72.  
  73. (defvar list-command-history-filter 'default-command-history-filter
  74.   "If non-nil, should be the name of a function of one argument.
  75. It is passed each element of the command history when
  76. \\[list-command-history] is called.  If the filter returns non-nil for
  77. some element, that element is excluded from the history listing.  The
  78. default filter removes commands associated with the command-history.")
  79.  
  80. (defun default-command-history-filter (frob)
  81.   "Filter commands matching `default-command-history-filter-garbage' list
  82. from the command history."
  83.   (or (not (consp frob))
  84.       (memq (car frob) default-command-history-filter-garbage)))
  85.  
  86. (defvar list-command-history-max 32
  87.   "*If non-nil, should be a positive number which specifies the maximum
  88. length of the Command History listing produced by `list-command-history'.")
  89.  
  90. ;;;###autoload
  91. (defun list-command-history ()
  92.   "List history of commands typed to minibuffer.
  93. The number of commands listed is controlled by `list-command-history-max'.
  94. Calls value of `list-command-history-filter' (if non-nil) on each history
  95. element to judge if that element should be excluded from the list.
  96.  
  97. The buffer is left in Command History mode."
  98.   (interactive)
  99.   (with-output-to-temp-buffer
  100.       "*Command History*"
  101.     (let ((history command-history)
  102.       (buffer-read-only nil)
  103.       (count (or list-command-history-max -1)))
  104.       (while (and (/= count 0) history)
  105.     (if (and (boundp 'list-command-history-filter)
  106.          list-command-history-filter
  107.          (funcall list-command-history-filter (car history)))
  108.         nil
  109.       (setq count (1- count))
  110.       (prin1 (car history))
  111.       (terpri))
  112.     (setq history (cdr history))))
  113.     (save-excursion
  114.       (set-buffer "*Command History*")
  115.       (goto-char (point-min))
  116.       (if (eobp)
  117.       (error "No command history")
  118.     (Command-history-setup)))))
  119.  
  120. (defun Command-history-setup (&optional majormode modename keymap)
  121.   (set-buffer "*Command History*")
  122.   (use-local-map (or keymap command-history-map))
  123.   (lisp-mode-variables nil)
  124.   (set-syntax-table emacs-lisp-mode-syntax-table)
  125.   (setq buffer-read-only t)
  126.   (use-local-map (or keymap command-history-map))
  127.   (setq major-mode (or majormode 'command-history-mode))
  128.   (setq mode-name (or modename "Command History")))
  129.  
  130. (defvar command-history-hook nil
  131.   "If non-nil, its value is called on entry to `command-history-mode'.")
  132.  
  133. (defvar command-history-map nil)
  134. (if command-history-map
  135.     nil
  136.   (setq command-history-map
  137.     (nconc (make-sparse-keymap) shared-lisp-mode-map))
  138.   (suppress-keymap command-history-map)
  139.   (define-key command-history-map "x" 'command-history-repeat)
  140.   (define-key command-history-map "\n" 'next-line)
  141.   (define-key command-history-map "\r" 'next-line)
  142.   (define-key command-history-map "\177" 'previous-line))
  143.  
  144. (defun command-history-repeat ()
  145.   "Repeat the command shown on the current line.
  146. The buffer for that command is the previous current buffer."
  147.   (interactive)
  148.   (save-excursion
  149.     (eval (prog1
  150.           (save-excursion
  151.         (beginning-of-line)
  152.         (read (current-buffer)))
  153.         (set-buffer
  154.          (car (cdr (buffer-list))))))))
  155.  
  156. ;;;###autoload
  157. (defun command-history-mode ()
  158.   "Major mode for examining commands from `command-history'.
  159. The number of commands listed is controlled by `list-command-history-max'.
  160. The command history is filtered by `list-command-history-filter' if non-nil.
  161. Use \\<command-history-map>\\[command-history-repeat] to repeat the command on the current line.
  162.  
  163. Otherwise much like Emacs-Lisp Mode except that there is no self-insertion
  164. and digits provide prefix arguments.  Tab does not indent.
  165. \\{command-history-map}
  166. Calls the value of `command-history-hook' if that is non-nil.
  167. The Command History listing is recomputed each time this mode is invoked."
  168.   (interactive)
  169.   (list-command-history)
  170.   (pop-to-buffer "*Command History*")
  171.   (run-hooks 'command-history-hook))
  172.  
  173. (provide 'chistory)
  174.  
  175. ;;; chistory.el ends here
  176.